package client.core.io;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import proprietes.ConnectProprietes;
import client.core.io.conection.ClientToClient;
import client.ui.workform.DialogForm;

public class Listner extends Thread {
	private Listner(){
		
	}
	public static Listner getInstance(){
		if(listner == null)
			listner = new Listner();
		new Thread(listner).start();
		return listner;
	}
	private static Listner listner= null;
	private ServerSocket serverSocket;
	protected Thread runningThread = null;
	private DialogForm textArea = null;
	public void run() {
		synchronized (this) {
			this.runningThread = Thread.currentThread();
		}
		openServerSocket();
		while (true) {
			Socket clientSocket = null;
			try {
				clientSocket = this.serverSocket.accept();
			} catch (IOException e) {
				throw new RuntimeException("Error accepting client connection",
						e);
			}
			ClientToClient client = new ClientToClient(clientSocket);
			new Thread(client).start();
		}
	}

	private void openServerSocket() {
		try {
			this.serverSocket = new ServerSocket(ConnectProprietes.CLIENT_PORT.getiValue());
		} catch (IOException e) {
			throw new RuntimeException("Cannot open port 8080", e);
		}
	}
	public void  setJTextArea(DialogForm textArea){
		this.textArea = textArea;
	}
}
